package com.example.demo.service.impl;

import com.example.demo.common.Constant;
import com.example.demo.dao.ActivityRepository;
import com.example.demo.dao.AdRepository;
import com.example.demo.dao.ApplyRepository;
import com.example.demo.model.entity.ActivityModel;
import com.example.demo.model.entity.AdModel;
import com.example.demo.model.entity.ApplyModel;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.UUID;

@Service
public class ActivityService {

    @Resource
    ActivityRepository activityRepository;

    @Resource
    ApplyRepository applyRepository;

    public ActivityModel save(ActivityModel model) {
        return activityRepository.save(model);
    }

    public boolean hasApply(String uid, String activityId) {
        ApplyModel apply = applyRepository.findByUserIdAndActivityId(uid, activityId);
        if (apply != null) {
            return true;
        }
        return false;
    }


    public Page<ActivityModel> getActivityPage(Specification specification, Pageable pageable) {
        return activityRepository.findAll(specification, pageable);
    }


    public ActivityModel getById(String activityId) {
        return activityRepository.findOne(activityId);
    }


    public void delete(String activityId) {
        activityRepository.delete(activityId);
    }

    public void apply(String activityId, String userId) {
        ActivityModel activityModel = activityRepository.findOne(activityId);
        if (activityModel == null) {
            throw new RuntimeException(MessageFormat.format(Constant.ERROR_FORMAT, 400, "活动不存在"));
        }
        if (activityModel.getActivityStatus() == Constant.ACTIVITY_END || activityModel.getEndTime() < System.currentTimeMillis()) {
            throw new RuntimeException(MessageFormat.format(Constant.ERROR_FORMAT, 400, "活动已结束"));
        }
        if (activityModel.getApplyCount() == activityModel.getLimitPeople()) {
            throw new RuntimeException(MessageFormat.format(Constant.ERROR_FORMAT, 400, "报名人数已满"));
        }
        if (!hasApply(userId, activityId)) {
            ApplyModel apply = new ApplyModel();
            apply.setCreateTime(System.currentTimeMillis());
            apply.setApplyId(UUID.randomUUID().toString());
            apply.setActivityId(activityId);
            apply.setUserId(userId);
            applyRepository.save(apply);
            activityModel.setApplyCount(activityModel.getApplyCount() + 1);
            activityRepository.save(activityModel);
        } else {
            throw new RuntimeException(MessageFormat.format(Constant.ERROR_FORMAT, 400, "你已经报过名了"));
        }


    }


    public void sign(String activityId, String userId) {
        ActivityModel activityModel = activityRepository.findOne(activityId);
        if (activityModel == null) {
            throw new RuntimeException(MessageFormat.format(Constant.ERROR_FORMAT, 400, "活动不存在"));
        }
        if (activityModel.getActivityStatus() == Constant.ACTIVITY_END || activityModel.getEndTime() < System.currentTimeMillis()) {
            throw new RuntimeException(MessageFormat.format(Constant.ERROR_FORMAT, 400, "活动已结束"));
        }
        ApplyModel apply = applyRepository.findByUserIdAndActivityId(userId, activityId);
        if (apply == null) {
            throw new RuntimeException(MessageFormat.format(Constant.ERROR_FORMAT, 400, "活动未报名"));
        }
        apply.setIsSign(1);
        apply.setSignTime(System.currentTimeMillis());
        applyRepository.save(apply);
    }
}
